package com;

import util.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserInfojustify  extends HttpServlet {

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        session.setMaxInactiveInterval(60*30);

        String sql = "select *from user where username=? and password=?";
        ArrayList list = new ArrayList();
        list.add(username);
        list.add(password);
        DBUtils db = new DBUtils();
        ResultSet rs = db.query(sql, list);
        try {
            if(rs.next()){
                session.setAttribute("a",username);
                session.setAttribute("b",password);
                resp.sendRedirect("success.jsp");
            }else if(!rs.next()){
                Cookie cookie = new Cookie("username",username);
                resp.addCookie(cookie);
                cookie.setMaxAge(60*60*24*30);
                resp.sendRedirect("login.jsp");
            }



        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            db.closeall();
        }

    }
}
